/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.magri.mvc.modelo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.magri.mvc.annotation.Bean;
import org.magri.mvc.annotation.BeanField;

/**
 *
 * @author Xand
 */
public abstract class AbstractBean {
    
    public AbstractBeanState estado;
    
    public abstract Integer getId();

    public abstract void setId(Integer id);
    
    /*
     * metodo que invoca o metodo set do field
     * 
     */
    public void set(String field, Object value) {
        //TODO obs: ainda tem que criar um cache de metodos para agilizar
        Bean bean = getClass().getAnnotation(Bean.class);
        
        System.out.println("" + bean.table());
        
        Field[] declaredFields = getClass().getDeclaredFields();
        
        for (Field declaredField : declaredFields) {
            BeanField annotationField = declaredField.getAnnotation(BeanField.class);
            
            if (annotationField.field().equals(field)) {
                System.out.println("field: " + annotationField.field() + " type: " + annotationField.type());
                try {
                    String declaredFieldName = declaredField.getName();
                    String nomeMetodo = declaredFieldName.substring(1, declaredFieldName.length());
                    nomeMetodo = "set" + declaredFieldName.substring(0, 1).toUpperCase() + nomeMetodo;
                    System.out.println("AbstractBean set: metodo: " + nomeMetodo);
                    Method declaredMethod = getClass().getDeclaredMethod(nomeMetodo, annotationField.type().getType());
                    
                    declaredMethod.invoke(this, value);
                } catch (Exception ex) {
                    Logger.getLogger(AbstractBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public Object get(String field) {
       
        Field[] declaredFields = getClass().getDeclaredFields();
        
        for (Field declaredField : declaredFields) {
            BeanField annotationField = declaredField.getAnnotation(BeanField.class);
            
            if (annotationField.field().equals(field)) {
                System.out.println("field: " + annotationField.field() + " type: " + annotationField.type());
                try {
                    String declaredFieldName = declaredField.getName();
                    String nomeMetodo = declaredFieldName.substring(1, declaredFieldName.length());
                    nomeMetodo = "get" + declaredFieldName.substring(0, 1).toUpperCase() + nomeMetodo;
                    System.out.println("AbstractBean set: metodo: " + nomeMetodo);
                    
                    Method declaredMethod  = getClass().getDeclaredMethod(nomeMetodo, null);
                    return declaredMethod.invoke(this);
                } catch (Exception ex) {
                    Logger.getLogger(AbstractBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return null;
    }
    
}
